package com.paobuji.aidebao.myapplication.ui;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jaeger.library.StatusBarUtil;
import com.paobuji.aidebao.myapplication.R;
import com.paobuji.aidebao.myapplication.activity.BaseActivity;
import com.paobuji.aidebao.myapplication.adapter.MyHistoryAdapter;
import com.paobuji.aidebao.myapplication.appinterface.GetInterfaceObjctListener;
import com.paobuji.aidebao.myapplication.bean.GetSportHistoryList;
import com.paobuji.aidebao.myapplication.bean.UserBeanResult;
import com.paobuji.aidebao.myapplication.tool.ACache;
import com.paobuji.aidebao.myapplication.tool.GetAppInfoUtil;
import com.paobuji.aidebao.myapplication.tool.MyIntentTools;
import com.paobuji.aidebao.myapplication.view.CustomTitlebar;

import org.json.JSONArray;

import java.text.DecimalFormat;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MySportHistoryActivity extends BaseActivity {

    @BindView(R.id.mainTitle)
    CustomTitlebar mainTitle;
    @BindView(R.id.myCor)
    TextView myCor;
    @BindView(R.id.myTime)
    TextView myTime;
    @BindView(R.id.myDis)
    TextView myDis;
    @BindView(R.id.historyMore)
    RecyclerView historyMore;

    MyHistoryAdapter mAdapter;
    UserBeanResult.DataBean.UserBean userBean;
    ACache aCache;
    Context context;
    MyIntentTools myIntentTools;
    MyIntentTools myIntentToolshis;
    DecimalFormat decimalFormat;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_sport_history);
        ButterKnife.bind(this);
        //设置全透明
        StatusBarUtil.setTransparent(this);
        context = getApplicationContext();
        decimalFormat = new DecimalFormat("0.00");//构造方法的字符格式这里如果小数不足2位,会以0补足.
        aCache = ACache.get(context);
        myIntentTools = new MyIntentTools();
        myIntentToolshis = new MyIntentTools();

        //在缓存中获取用户数据
        userBean = (UserBeanResult.DataBean.UserBean) aCache.getAsObject("userBean");
        Gson gson = new Gson();
        String s = gson.toJson(userBean);
        Log.e("11111", "initData: "+ s);
        //判断用户数据是否拿到，若没有的话从服务器拉取
        if(userBean == null){
//            myIntentTools.loginUserByIdentified(context, GetAppInfoUtil.getUniquePsuedoID());
//            myIntentTools.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
//                @Override
//                public void getObjectDate(Object data) {
//                    userBean = ((UserBeanResult)data).getData().getUser();
//                    setViewText();
//                }
//            });
            userBean = new UserBeanResult.DataBean.UserBean();
            setViewText();
        }else{
            setViewText();
        }
        //标题栏事件监测
        mainTitle.setAction(new CustomTitlebar.TitleBarOnClickListener() {
            @Override
            public void performAction(View view) {
                switch (view.getId()) {
                    case R.id.iv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "左边图片按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_left:
                        finish();
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.iv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.tv_right:
                        //Toast.makeText(MainActivity.this, "右边文字按钮", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        getData();
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    /**
     * 设置用户总数据
     */
    public void setViewText() {
        Log.i("时间",userBean.getAllTime()+"");
        double sportDisAll = aCache.getAsString("sportDisAll") == null ? 0 : Double.parseDouble(aCache.getAsString("sportDisAll"));
        int sportTimeAll = aCache.getAsString("sportTimeAll") == null ? 0 : Integer.parseInt(aCache.getAsString("sportTimeAll"));
        int sportCorAll = aCache.getAsString("sportCorAll") == null ? 0 :  Integer.parseInt(aCache.getAsString("sportCorAll"));
        myCor.setText(sportCorAll+"");
        myTime.setText(getTime(Integer.parseInt(String.valueOf(sportTimeAll))));
        myDis.setText(String.format("%.2f", sportDisAll));


    }

    /**
     * 获取用户数据
     */
    public void getData(){

        Gson gson = new Gson();
        ArrayList<GetSportHistoryList.DataBean.SporthitorylistBean> sporthitorylist = (ArrayList<GetSportHistoryList.DataBean.SporthitorylistBean>) aCache.getAsObject("sporthitorylist");
        setRecycleView(sporthitorylist==null?new ArrayList():sporthitorylist);

                        String s = gson.toJson(sporthitorylist);
                Log.e("11111", "initData: "+ s);

//        myIntentToolshis.getSportHitoryList(context,userBean.getId());
        //myIntentToolshis.getSportHitoryList(context,2);
//        myIntentToolshis.setGetInterfaceObjctListener(new GetInterfaceObjctListener() {
//            @Override
//            public void getObjectDate(Object data) {
//
//                Gson gson = new Gson();
//                String s = gson.toJson(data);
//                Log.e("11111", "initData: "+ s);
//
//                setRecycleView((ArrayList) ((GetSportHistoryList)data).getData().getSporthitorylist());
//            }
//        });
    }

    /**
     *
     */
    public void setRecycleView(ArrayList arrayList) {
        historyMore.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
        mAdapter = new MyHistoryAdapter(context,arrayList);
        historyMore.setAdapter(mAdapter);


    }

    public static String getTime(int second) {
        if (second < 10) {
            return "00:0" + second;
        }
        if (second < 60) {
            return "00:" + second;
        }
        if (second < 3600) {
            int minute = second / 60; second = second - minute * 60;
            if (minute < 10) {
                if (second < 10) {
                    return "0" + minute + ":0" + second;
                } return "0" + minute + ":" + second;
            }
            if (second < 10) {
                return minute + ":0" + second;
            }
            return minute + ":" + second;
        }
        int hour = second / 3600;
        int minute = (second - hour * 3600) / 60;
        second = second - hour * 3600 - minute * 60;
        if (hour < 10) {
            if (minute < 10) {
                if (second < 10) {
                    return "0" + hour + ":0" + minute + ":0" + second;
                }
                return "0" + hour + ":0" + minute + ":" + second;
            }
            if (second < 10) {
                return "0" + hour + minute + ":0" + second;
            } return "0" + hour + minute + ":" + second;
        }
        if (minute < 10) {
            if (second < 10) {
                return hour + ":0" + minute + ":0" + second;
            }
            return hour + ":0" + minute + ":" + second;
        }
        if (second < 10) {
            return hour + minute + ":0" + second;
        }
        return hour + minute + ":" + second;
    }

}
